<?php
require_once('IRepositorioNewsLetter.php');
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RepositorioNewsLetter
 *
 * @author Paulo
 */
class RepositorioNewsLetter implements IRepositorioNewsLetter {
    private $con = null;


    function __construct() {
        $this->con = new Conexao();
    }

    public function cadastrarEmail($email) {
        $retorno = false;
        $this->con->abrirConexao();

        $query = "Insert Into Newsletter(email) values('". $email ."')";

        if($this->con->executarQuery($query)) {//retorna true caso o usuario seja inserido com sucesso no banco.
            $retorno = true;
        }

        $this->con->fecharConexao();
        return $retorno;
    }

    public function removerEmail($email) {
        $retorno = false;
        $this->con->abrirConexao();

        $query = "delete from newsletter where email = '".$email."'";
        
        $return = $this->con->executarQuery($query);

        if($return) {
            $retorno = true;
        }

        $this->con->fecharConexao();
        return $retorno;
    }

    public function verificarEmail($email) {
        $resultado = true;

        $this->con->abrirConexao();

        $query = "select * from Newsletter where email='$email';";

        $e = $this->con->executarQuery($query);

        if(mysql_num_rows($e) != 0) {
            $resultado = false;
        }

        $this->con->fecharConexao();
        return $resultado;
    }
    public function enviarNewsLetter($newsLetter){
        $newsLetter = new NewsLetter();

        $resultado = false;

        $this->con->abrirConexao();

        $query = mysql_query("Select * from newsletter");

        $this->con->executarQuery($query);

        if($this->con->executarQuery($query)){
           $returno1 = true;
        }

        while($valor = mysql_fetch_array($query)){

            $destino = "$valor[email]";

            $email = mail("$destino", $newsLetter->getAssunto(), $newsLetter->getMensagem()
                , "From: willamissantos@hotmail.com");

            if($mail){
                $resultado = true;
            }else{
                $resultado = false;
            }
        }
        $this->con->fecharConexao();
        return $resultado;
    }
}
?>
